package com.csw.mysqldate.Transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("Transation")
public class PersonTransation {

    @Autowired
    private TransactionTemplate template;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @RequestMapping("show")
    public void show() {
        PlatformTransactionManager transactionManager = template.getTransactionManager();
        TransactionDefinition definition = new DefaultTransactionDefinition();
        TransactionStatus transactionStatus = transactionManager.getTransaction(definition);

        try {
            String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
            extracted1(date);
            int i=1/0;
        } catch (TransactionException e) {
            transactionManager.rollback(transactionStatus);
            throw e;
        }

        transactionManager.commit(transactionStatus);
    }

    private void extracted1(String date) {
        jdbcTemplate.execute("insert into people(create_time,end_time) values ('" + date + "','" + date + "')");
    }
}

//————————————————
//        版权声明：本文为CSDN博主「魔道不误砍柴功」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/qq_35971258/article/details/128620476
